package com.ruoyi.web.controller.reservation;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.Reservation;
import com.ruoyi.system.domain.Room;
import com.ruoyi.system.service.IReservationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * 房间预约Controller
 * 
 * @author ruoyi
 * @date 2025-05-18
 */
@RestController
@RequestMapping("/reservation/reservation")
public class ReservationController extends BaseController
{
    @Autowired
    private IReservationService reservationService;

    /**
     * 查询房间预约列表
     */
    @PreAuthorize("@ss.hasPermi('reservation:reservation:list')")
    @GetMapping("/list")
    public TableDataInfo list(Reservation reservation,@RequestParam(value = "roomName", required = false) String roomName,@RequestParam(value = "nickName", required = false) String nickName)
    {
        startPage();
        if(roomName!=null){
            if (reservation.getRoom() == null) {
                reservation.setRoom(new Room());
            }
            reservation.getRoom().setName(roomName);
        }
        if(nickName!=null){
            if (reservation.getSysUser() == null) {
                reservation.setSysUser(new SysUser());
            }
            reservation.getSysUser().setNickName(nickName);
        }
        System.out.println(reservation);
        List<Reservation> list = reservationService.selectReservationList(reservation);
        return getDataTable(list);
    }

    /**
     * 导出房间预约列表
     */
    @PreAuthorize("@ss.hasPermi('reservation:reservation:export')")
    @Log(title = "房间预约", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Reservation reservation)
    {
        List<Reservation> list = reservationService.selectReservationList(reservation);
        ExcelUtil<Reservation> util = new ExcelUtil<Reservation>(Reservation.class);
        util.exportExcel(response, list, "房间预约数据");
    }

    /**
     * 获取房间预约详细信息
     */
    @PreAuthorize("@ss.hasPermi('reservation:reservation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(reservationService.selectReservationById(id));
    }

    /**
     * 新增房间预约
     */
    @PreAuthorize("@ss.hasPermi('reservation:reservation:add')")
    @Log(title = "房间预约", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Reservation reservation)
    {
        System.out.println("新增预约");
        return toAjax(reservationService.insertReservation(reservation));
    }

    /**
     * 修改房间预约
     */
    @PreAuthorize("@ss.hasPermi('reservation:reservation:edit')")
    @Log(title = "房间预约", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Reservation reservation)
    {
        return toAjax(reservationService.updateReservation(reservation));
    }

    /**
     * 删除房间预约
     */
    @PreAuthorize("@ss.hasPermi('reservation:reservation:remove')")
    @Log(title = "房间预约", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(reservationService.deleteReservationByIds(ids));
    }
//    查询该日有多少订单
@GetMapping("/booked-hours")
public TableDataInfo  getBookedHours(
        @RequestParam Long roomId,
        @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date) {
    List<Integer> data = reservationService.getBookedHoursManually(roomId, date);
    return getDataTable(data);
}
//新增预约
    @PostMapping("/submit")
    public  AjaxResult addReservation(
           @RequestBody Reservation reservation
    ){


        Long userId = SecurityUtils.getUserId();
        reservation.setUserId(userId);

        reservation.setStatus(0L);
        int rows = reservationService.insertReservation(reservation);
        if (rows > 0) {
            // 插入成功后，reservation对象的id属性会被MyBatis自动填充
            return AjaxResult.success("操作成功", reservation.getId());
        }
        return AjaxResult.error("操作失败");
    }
}
